/*
 * @Author: ZAL 2177624751@qq.com
 * @Date: 2025-02-05 15:07:57
 * @LastEditors: ZAL 2177624751@qq.com
 * @LastEditTime: 2025-02-05 15:08:08
 * @FilePath: \asset-management-webc:\Users\21776\Desktop\毕设\shop_app_vue3\store\orderStore.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
//订单数据

import { defineStore } from 'pinia';
import {
  createOrder,
  payOrder,
  cancelOrder,
  confirmOrder,
  getOrderDetail,
  getOrderList
} from '../api/order.js';

export const useOrderStore = defineStore('order', {
  state: () => ({
    // 订单列表
    orderList: [],
    // 当前订单详情
    currentOrder: null,
    // 当前订单状态筛选
    currentStatus: '',
    // 加载状态
    loading: false
  }),

  getters: {
    // 获取订单列表
    getOrderList: (state) => state.orderList,
    // 获取当前订单详情
    getCurrentOrder: (state) => state.currentOrder,
    // 获取当前订单状态筛选
    getCurrentStatus: (state) => state.currentStatus,
    // 获取加载状态
    isLoading: (state) => state.loading
  },

  actions: {
    // 创建订单
    async submitOrder(orderCreateDTO) {
      if (this.loading) return;
      
      try {
        this.loading = true;
        const res = await createOrder(orderCreateDTO);
        if (res.code === 200) {
          await this.loadOrderList();
          return res.data;
        }
        return res;
      } catch (error) {
        throw error;
      } finally {
        this.loading = false;
      }
    },

    // 支付订单
    async payOrder(orderNo) {
      try {
        this.loading = true;
        const res = await payOrder(orderNo);
        if (res.code === 200) {
          await this.loadOrderDetail(orderNo);
          await this.loadOrderList();
        }
        return res;
      } catch (error) {
        throw error;
      } finally {
        this.loading = false;
      }
    },

    // 取消订单
    async cancelOrder(orderNo) {
      try {
        this.loading = true;
        const res = await cancelOrder(orderNo);
        if (res.code === 200) {
          // 刷新订单详情和列表
          await this.loadOrderDetail(orderNo);
          await this.loadOrderList({ status: this.currentStatus });
        }
        return res;
      } catch (error) {
        throw error;
      } finally {
        this.loading = false;
      }
    },

    // 确认收货
    async confirmOrder(orderNo) {
      try {
        this.loading = true;
        const res = await confirmOrder(orderNo);
        if (res.code === 200) {
          await this.loadOrderDetail(orderNo);
          await this.loadOrderList();
        }
        return res;
      } catch (error) {
        throw error;
      } finally {
        this.loading = false;
      }
    },

    // 加载订单详情
    async loadOrderDetail(orderNo) {
      if (this.loading) return;
      
      try {
        this.loading = true;
        const res = await getOrderDetail(orderNo);
        if (res.code === 200) {
          this.currentOrder = res.data;
        }
        return res;
      } catch (error) {
        throw error;
      } finally {
        this.loading = false;
      }
    },

    // 加载订单列表
    async loadOrderList(params = {}) {
      if (this.loading) return;
      
      try {
        this.loading = true;
        const { status = '' } = params;
        
        // 更新当前状态
        this.currentStatus = status;

        // 调用API获取订单列表
        const res = await getOrderList({ status });
        
        if (res.code === 200) {
          this.orderList = res.data || [];
        }
        return res;
      } catch (error) {
        throw error;
      } finally {
        this.loading = false;
      }
    },

    // 清除当前订单详情
    clearCurrentOrder() {
      this.currentOrder = null;
    },

    // 重置订单列表
    resetOrderList() {
      this.orderList = [];
      this.currentStatus = '';
    }
  }
});